﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Collections.ObjectModel;
using CodeSample.Model;
using CodeSample.Utils;

namespace CodeSample
{
    /// <summary>
    /// Interaction logic for MergingWindow.xaml
    /// </summary>
    public partial class MergingWindow : Window
    {
        private volatile static MergingWindow instance = null;
        private DataProcessMan dataProcess;
        private DataProcessMan dataProcess2;
        private ObservableCollection<SampleMan> samples;
        private ObservableCollection<SampleMan> samples2;
        private ObservableCollection<SampleMan> differences;
        private ObservableCollection<SampleMan> differentInVersion;
        public delegate void MergeCallBack(string statusMessage);
        public MergeCallBack mergeCallBack;

        private MergingWindow(MergeCallBack mergeCallBack)
        {
            InitializeComponent();
            this.mergeCallBack = mergeCallBack;
        }

        public static MergingWindow GetInstance(MergeCallBack mergeCallBack)
        {
            if (instance == null)
            {
                Mutex mutex = new Mutex();
                mutex.WaitOne();

                if (instance == null)
                    instance = new MergingWindow(mergeCallBack);

                mutex.Close();
            }
            return instance;
        }

        public static bool IsIstanceNull
        { 
            get { return instance == null; }
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "CodeSample Database"; // Default file name
            dlg.DefaultExt = ".mdb"; // Default file extension
            dlg.Filter = "MS Access 2003 (.mdb) |*.mdb"; // Filter files by extension

            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                this.txtFilePath.Text = dlg.FileName;
                //this.dataProcess = new DataProcess();
                //this.dataProcess2 = new DataProcess(dlg.FileName);

                //this.samples = new ObservableCollection<Sample>();
                //this.samples2 = new ObservableCollection<Sample>();

                //this.dataProcess.GetSamples(ref this.samples);
                //this.dataProcess2.GetSamples(ref this.samples2);

                //this.differences = this.GetDifferences(samples, samples2);
                //MessageBox.Show("There are " + this.differences.Count + " new sample(s)");

                //this.SaveDifferences(this.differences, this.dataProcess);
            }
        }

        private ObservableCollection<SampleMan> GetDifferences(ObservableCollection<SampleMan> samples, ObservableCollection<SampleMan> samples2)
        {
            ObservableCollection<SampleMan> differences = new ObservableCollection<SampleMan>();

            foreach (SampleMan aSample in samples2)
            {
                if (!this.IsSamplesListContains(samples, aSample))
                    differences.Add(aSample);
            }

            return differences;
        }

        private bool IsSamplesListContains(ObservableCollection<SampleMan> samples, SampleMan otherSample)
        {
            foreach (SampleMan aSample in samples)
            {
                if (otherSample.IsIDTheSame(aSample))
                    return true;
            }

            return false;
        }

        private void SaveDifferences(ObservableCollection<SampleMan> samples, DataProcessMan dataProcess)
        {
            foreach (SampleMan aSample in samples)
            {
                dataProcess.AddNewSample(aSample, false);
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            instance = null;
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

        private void btnAnalyze_Click(object sender, RoutedEventArgs e)
        {
            this.dataProcess = new DataProcessMan();
            this.dataProcess2 = new DataProcessMan(this.txtFilePath.Text);

            this.samples = new ObservableCollection<SampleMan>();
            this.samples2 = new ObservableCollection<SampleMan>();

            this.dataProcess.GetSamples(ref this.samples);
            this.dataProcess2.GetSamples(ref this.samples2);

            this.differences = this.GetDifferences(samples, samples2);

            this.txtBlkTotal.Text = this.samples2.Count.ToString();
            this.txtBlkIsNew.Text = this.differences.Count.ToString();
            //this.txtBlkIsDifferent.Text = (this.samples2.Count - this.differences.Count).ToString();
        }

        private void btnAddNewSampleToList_Click(object sender, RoutedEventArgs e)
        {
            this.SaveDifferences(this.differences, this.dataProcess);
            this.Close();
            this.mergeCallBack("Successfully added " + this.differences.Count + " sample(s) into your list");
        }
    }
}
